import {
    getTeacherGradeClases,
    getStudentsAndReviewList,
    getStudentsReviewType,
    delReviewTypeList,
    postStudentReview,
    postReviewType,
    putReviewType,
    getIconList,
    getStudentArchives,
    getClassStudentInfoList,
    getStudentInfoList,
    delStudentReview,
    getStudentInfo
} from '@/api/studentAppraiseApi';
import { valHooks } from 'jquery';
const studentAppraise = {
    namespaced: true,
    state: {
        //当前页[0:index,1:得分明细] 
        currentPage:localStorage.currentPage ? JSON.parse(localStorage.currentPage) : '',  
        //indexList
        scoreOrderType:null,
        timerType:localStorage.timerType ? JSON.parse(localStorage.timerType) : 0,
        //是否是全班
        isAllStudent:localStorage.isAllStudent ? JSON.parse(localStorage.isAllStudent) : false,
        //indexList时间
        startTime:null,
        endTime:null,
        //scoreInfo时间
        scoreInfo_startTime:null,
        scoreInfo_endTime:null,
        scoreInfo_timeType:1,
        //评价
        evaluateInfo:null,
        //1.表扬;2.批评
        evaluateType:localStorage.evaluateType ? JSON.parse(localStorage.evaluateType) : '',  
        //学生信息
        studentsInfo: localStorage.studentsInfo ? JSON.parse(localStorage.studentsInfo) : '',
        //选择班级
        classList:[],
        classItem:localStorage.classItem ? JSON.parse(localStorage.classItem) : '',
        //添加表扬
        addPraiseImgUrl:localStorage.addPraiseImgUrl ? JSON.parse(localStorage.addPraiseImgUrl) : '',
    },
    getters: {
        
    },
    mutations: {
        setScoreInfo_timeType(state,val){
            state.scoreInfo_timeType = val;
            localStorage.scoreInfo_timeType = JSON.stringify(val);
        },
        setIsAllStudent(state,val){
            state.isAllStudent = val;
            localStorage.isAllStudent = JSON.stringify(val);
        },
        setEvaluateType(state,val){
            state.evaluateType = val;
            localStorage.evaluateType = JSON.stringify(val);
        },
        setAddPraiseImgUrl(state, val){
            state.addPraiseImgUrl = val;
            //state.addPraise = JSON.parse(JSON.stringify(state.addPraise))
            localStorage.addPraiseImgUrl = JSON.stringify(val);
        },
        setStudentsInfo(state, obj){
            state.studentsInfo = obj;
            localStorage.studentsInfo = JSON.stringify(obj);
        },
        setClassItem(state, obj){
            state.classItem = obj;
            localStorage.classItem = JSON.stringify(obj);
        },
        setScoreOrderType(state, val){
            state.scoreOrderType= val;
            localStorage.scoreOrderType = JSON.stringify(val);
        },
        setCurrentPage(state, val){
            state.currentPage= val;
            localStorage.currentPage = JSON.stringify(val);
        },
        setTimerType(state, val){
            state.timerType = val;
            localStorage.timerType  = JSON.stringify(val);
        }
    },
    actions: {
        //获取教师授课年级班级
        GetTeacherGradeClases ({ state, rootState, commit, dispatch }) {
            const id = rootState.user.teacherId; 
            return new Promise((resolve, reject) => {
                getTeacherGradeClases(id).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        //获取班级学生列表（含学生点评数据）
        GetStudentsAndReviewList({ state, rootState, commit, dispatch }) {    
            const params = {
                schoolId:rootState.user.schoolId,
                classId:rootState.user.classId,
                scoreOrderType:state.scoreOrderType,
                DefaultTimeType:state.timerType,
                startTime:state.startTime,
                endTime:state.endTime
            }

            if(state.classItem){
                params.classId = state.classItem.classId;
            }

            if(state.endTime){
                params.endTime = state.endTime + ' 23:59:59';
                params.DefaultTimeType = String(state.timerType)
            }

            if(state.timerType != 0){
                params.startTime = null;
                params.endTime = null;
            }

            for (var key in params) {
                if (!params[key]) {
                    delete params[key]
                }
            }

            return new Promise((resolve, reject) => {
                getStudentsAndReviewList(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //学生点评类别 --- 获取列表
        GetStudentsReviewType({ state, rootState, commit, dispatch },params) {
            return new Promise((resolve, reject) => {
                getStudentsReviewType(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //学生点评类别 --- 批量删除
        DelReviewTypeList({ state, rootState, commit, dispatch },params) {
            return new Promise((resolve, reject) => {
                delReviewTypeList(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //添加点评
        PostStudentReview({ state, rootState, commit, dispatch },reviewText) {
            let reviewTypeId = state.evaluateInfo ? state.evaluateInfo.id : '';
            if(reviewText){
                reviewTypeId = null;
            }
            const params = {
                schoolId:rootState.user.schoolId,
                gradeId:state.classItem.gradeId,
                classId:state.classItem.classId,
                teacherId:rootState.user.teacherId,
                studentIds:state.isAllStudent ? state.studentsInfo.ids : [state.studentsInfo.id],
                reviewTypeId,
                reviewText
            }
            for (var key in params) {
                if (!params[key]) {
                    delete params[key]
                }
            }
            return new Promise((resolve, reject) => {
                postStudentReview(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //创建点评类别
        PostReviewType({ state, rootState, commit, dispatch },params) {
            return new Promise((resolve, reject) => {
                postReviewType(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //更新点评类别
        PutReviewType({ state, rootState, commit, dispatch },params) {
            return new Promise((resolve, reject) => {
                putReviewType(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //获取默认图标列表
        GetIconList({ state, rootState, commit, dispatch }) {
            return new Promise((resolve, reject) => {
                getIconList().then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //学生档案
        GetStudentArchives({ state, rootState, commit, dispatch }) {
            const params = {
                classId:state.classItem.classId,
                studentId:state.studentsInfo.id
            }
            return new Promise((resolve, reject) => {
                getStudentArchives(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //班级点评明细列表（移动端-教师-班级得分明细）
        GetClassStudentInfoList({ state, rootState, commit, dispatch }) {
            const params = {
                schoolId:rootState.user.schoolId,
                classId:state.classItem.classId,
                teacherId:rootState.user.teacherId,
                startTime:state.scoreInfo_startTime,
                endTime:state.scoreInfo_endTime,
                timeType:state.scoreInfo_timeType
            }
            for (var key in params) {
                if (!params[key]) {
                    delete params[key]
                }
            }
            return new Promise((resolve, reject) => {
                getClassStudentInfoList(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //学生点评明细列表
        GetStudentInfoList({ state, rootState, commit, dispatch }) {

            let classId = null;
            let studentId = null;
            let teacherId = null;
            // if(rootState.user.roleCode == 'Parent'){
            //     classId = rootState.user.classId
            //     studentId = rootState.user.studentId
            //     teacherId = null
            // }else{
            //     classId = state.classItem.classId
            //     studentId = state.studentsInfo.id
            //     teacherId = rootState.user.teacherId
            // }

            if(rootState.user.roleCode == 'Parent'){
                classId = state.classItem.classId
                studentId = state.studentsInfo.id
                teacherId = null
            }else{
                classId = state.classItem.classId
                studentId = state.studentsInfo.id
                teacherId = rootState.user.teacherId
            }

            const params = {
                schoolId:rootState.user.schoolId,
                classId,
                studentId, 
                teacherId,
                startTime:state.scoreInfo_startTime,
                endTime:state.scoreInfo_endTime + ' 23:59:59',
                //timeType:state.scoreInfo_timeType
            }
            for (var key in params) {
                if (!params[key]) {
                    delete params[key]
                }
            }
            return new Promise((resolve, reject) => {
                getStudentInfoList(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //删除点评
        DelStudentReview({ state, rootState, commit, dispatch },params) {
            return new Promise((resolve, reject) => {
                delStudentReview(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        //获取学生
        GetStudentInfo({ state, rootState, commit, dispatch },id) {
            return new Promise((resolve, reject) => {
                getStudentInfo(id).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        }
    }
}
export default studentAppraise